/* 
 * File:   Rotate.cpp
 * Author: maximiliano
 * 
 * Created on March 26, 2013, 5:44 PM
 */

#include "Rotate.h"

using namespace TP3::Mangling;

Rotate::Rotate(int placesNumber) {
    _placesNumber = placesNumber;
}

string& Rotate::run (string& operating){
    
    int factor = (_placesNumber >= 0)? 1: -1;
    
    for (int x = 0; x < factor * _placesNumber; x ++){
        
        if (factor > 0)
            moveRight (operating);
        
        else if (factor < 0)
            moveLeft (operating);
    }
    
    return operating;
}

Rotate::~Rotate() {
}

string Rotate::getName(){
    
    return "rotate";
}

string& Rotate::moveLeft (string& operating){
    
    if (operating.length() > 1)
    {
        string aux = operating.substr(1, operating.length() -1);
        operating = aux + operating.c_str()[0];
    }
    
    return operating;
}

string& Rotate::moveRight (string& operating){

    if (operating.length() > 1)
    {
        string aux = operating.substr(0, operating.length()- 1);
        operating = operating.c_str()[operating.length() -1] + aux;
    }
    
    return operating;
}
